package com.tqz.dt.config;

import com.tqz.dt.advisor.GlobalTransactionAttributeSourceAdvisor;
import com.tqz.dt.interceptors.GlobalTransactionalInterceptor;
import com.tqz.dt.source.GlobalTransactionAttributeSource;
import com.tqz.dt.source.AnnotationGlobalTransactionAttributeSource;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;

/**
 * <p>事务配置类
 *
 * @author tianqingzhao
 * @since 2021/7/7 14:39
 */
@Configuration
public class GlobalTransactionConfiguration {

    @Bean
    public GlobalTransactionAttributeSource globalTransactionAttributeSource() {
        return new AnnotationGlobalTransactionAttributeSource();
    }

    @Bean
    public GlobalTransactionalInterceptor globalTransactionalInterceptor() {
        GlobalTransactionalInterceptor angleTransactionInterceptor = new GlobalTransactionalInterceptor();
        angleTransactionInterceptor.setAngleTransactionAttributeSource(globalTransactionAttributeSource());
        return angleTransactionInterceptor;
    }

    @Bean
    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
    public GlobalTransactionAttributeSourceAdvisor globalTransactionAttributeSourceAdvisor() {
        GlobalTransactionAttributeSourceAdvisor attributeSourceAdvisor = new GlobalTransactionAttributeSourceAdvisor();
//        attributeSourceAdvisor.setAngleTransactionAttributeSource(angleTransactionAttributeSource());
        attributeSourceAdvisor.setAdvice(globalTransactionalInterceptor());
        return attributeSourceAdvisor;
    }

}
